<?php
/*
用户模块，包括登录用户的增删改、银行管理、中介管理
用户权限添加完成20170110
*/
namespace Home\Controller;
use Think\Controller;
class UserController extends CommonController{


//用户列表
  public function index(){
    $con = array('isdel'=>'0');
    if($_GET['p']==''){
        $p='1';
    }else{
        $p=$_GET['p'];
    }
    $count=D('User')->countUser($con);
    $n=15;
    $page=new \Think\Page($count,$n);
    $show= $page->show();// 分页显示输出
    $this->assign('page',$show);
    $this->assign('UserNum',$count);
    $users = D('User')->selectUsers($con,$p,$n);
    $this->assign('users' ,$users);
    //p($users);
    $this->display('User/index');
  }

  public function search(){
    $username = I('post.username');
    if($username){
      $con = array('user_name'=>array('like' , '%'.$username.'%'),'isdel'=>'0');
    }
    else{
      $con = array('isdel'=>'0');
    }
    if($_GET['p']==''){
        $p='1';
      }else{
        $p=$_GET['p'];
      }
      $count=D('User')->countUser($con);
      $n=15;
      $page=new \Think\Page($count,$n);
      $show= $page->show();// 分页显示输出
      $this->assign('page',$show);// 赋值分页输出
    $users = D('User')->selectUsers($con,$p,$n);
    $this->assign('users',$users);
    $this->assign('UserNum',$count);
    $this->assign('searusername' , $username);
    $this->display('User/index');
  }

//添加用户页面
  public function memberadd(){
    $banks = D('Bank') -> selectBank(array('isdel'=>'0','pid'=>'0'));
    $this->assign('banks',$banks);
    $this -> assign('place' , C('place')) ;
    $roles = D('Level') -> selectLevel();
    $this-> assign('roles' , $roles);

    $this->display('User/member-add');
  }

//编辑用户的页面
  public function memberalert(){
    $id = I('get.id');
    $user = D('User') -> findUser(array('user_id'=>$id));
    $this->assign('user' , $user);
    $this -> assign('place' , C('place')) ;
    $roles = D('Level') -> selectLevel();
    $this-> assign('roles' , $roles);
    $banks = D('Bank') -> selectBank(array('isdel'=>'0','pid'=>'0'));
    $this->assign('banks',$banks);
    $user = D('User')->selectUsers(array('isdel'=>'0','leader'=>''));
    $this->assign('users',$user);
    $this->display('User/member-alert');
  }

  public function memberview(){
    $id = I('get.id');
    $user = D('User') -> findUser(array('user_id'=>$id));
    $this->assign('user' , $user);
    $this -> assign('place' , C('place')) ;
    $roles = D('Level') -> selectLevel();
    $this-> assign('roles' , $roles);

    $this->assign('banks',$banks);
    $this->display('User/member-view');
  }

//更改密码
  public function memberchangepassword(){
    $id = I('get.id');
    $user = D('User') -> findUser(array('user_id'=>$id));
    $this->assign('user' , $user);
    $this->display('User/member-change-password');
  }




//添加用户检验
  public function addCheck(){
    if(IS_POST){
      $point_id = D('Point') -> addPoint('User/addCheck', '添加用户');//功能节点id
      $roleid = session('USER_AUTH.roleid');//权限角色id
      if( haveAblitity($point_id , $roleid) ){//判断功能节点id是否在功能id字符串里
        $username = I('post.username');
        $data['pwd'] = md5(I('post.password'));
        $data['phone'] = I('post.telphone');
        $data['place'] = I('post.place');
        $data['level'] = I('post.role');
        $data['oa_level'] = I('post.oaLevel');
        if(I('post.leader')){
            $data['leader'] = I('post.leader');
        }
        //$this -> jsonReply('500' , I('post.bank'));
        //当银行不为空时，判断选中的角色是否可以添加银行
        if(I('post.bank')){
          $point_id1 = D('Point') -> addPoint('User/addCheck/addbank', '添加审核人的银行');//功能节点id
          if( haveAblitity($point_id1 , $data['level']) ){
            $data['bank_id'] = implode(',',I('post.bank'));
          }
          else{
            $this -> jsonReply('500' , $point_id1);
          }
        }
        //判断用户名是否存在
        $findUsername = D('User') -> findSameUser($username);
        if($findUsername){
          $this -> jsonReply('500' , '用户名已存在');
        }
        else{
          $data['user_name'] = $username;
          if(trim($username)=='' || I('post.password')=='' || $data['place']=='' || $data['level']==''){
            $this -> jsonReply('500' , '请填写完整');
          }
          $add = D('User') -> addUser($data);
          if($add){
            $this -> jsonReply('200' , '添加成功');
          }
          else{
            $this -> jsonReply('500' , '添加失败');
          }
        }
      }
      else{
        $this -> jsonReply('500' , '您没有权限操作此功能');
      }

    }
  }

//编辑用户信息的检验
  public function editCheck(){
    if(IS_POST){
      $point_id = D('Point') -> addPoint('User/editCheck', '编辑用户');//功能节点id
      $roleid = session('USER_AUTH.roleid');//权限角色id
      if( haveAblitity($point_id , $roleid) ){
        $userid = I('post.userid');
        $user = D('User') -> findUser(array('user_id'=>$userid));
        $username = I('post.username');
        $data['phone'] = I('post.telphone');
        $data['place'] = I('post.place');
        $data['level'] = I('post.role');
        $data['oa_level'] = I('post.oaLevel');
        if(I('post.leader')){
            $data['leader'] = I('post.leader');
        }
        if(I('post.bank')){
          $point_id1 = D('Point') -> addPoint('User/addCheck/addbank', '添加审核人的银行');//功能节点id
          if( haveAblitity($point_id1 , $data['level']) ){
            $data['bank_id'] = implode(',',I('post.bank'));
          }
          else{
            $this -> jsonReply('500' , '请选择正确的角色操作银行');
          }
        }
        $findUsername = D('User') -> findSameUser($username,$userid);
        if($findUsername){
          $this -> jsonReply('500' , '用户名已存在');
        }
        else{
          if(trim($username)=='' ||  $data['place']=='' || $data['level']==''){
            $this -> jsonReply('500' , '请填写完整');
          }
          $data['user_name'] = $username;
          $add = D('User') -> saveUser($userid,$data);
          if($add){
            $this -> jsonReply('200' , '编辑成功');
          }
          else{
            $this -> jsonReply('500' , '请对内容进行修改');
          }
        }
      }
      else{
        $this -> jsonReply('500' , '您没有权限对用户进行修改');
      }
    }
  }

//修改密码的检验
  public function changeCheck(){
    if(IS_POST){
      $point_id = D('Point') -> addPoint('User/changeCheck', '修改密码');//功能节点id
      $roleid = session('USER_AUTH.roleid');//权限角色id
      if( haveAblitity($point_id , $roleid) ){
        $userid = I('post.userid');
        if( I('post.newpassword') == I('post.newpassword2') ){
          $data['pwd'] = md5(I('post.newpassword'));
          $add = D('User') -> saveUser($userid,$data);
          if($add){
            $this -> jsonReply('200' , '修改成功');
          }
          else{
            $this -> jsonReply('500' , '请填写与上次密码不同的新密码');
          }
        }
        else{
          $this -> jsonReply('500' , '两次密码必须一致');
        }
      }
      else{
        $this -> jsonReply('500' , '您没有权限操作此功能');
      }
    }
  }

public function change(){
    if(IS_POST){
      $point_id = D('Point') -> addPoint('User/change', '个人修改密码');//功能节点id
      $roleid = session('USER_AUTH.roleid');//权限角色id
      if( haveAblitity($point_id , $roleid) ){
        $userid = I('post.userid');
            $data['phone'] = I('post.telphone');
          $data['pwd'] = md5(I('post.password'));
          $add = D('User') -> saveUser($userid,$data);
          if($add){
            $this -> jsonReply('200' , '修改成功');
          }
          else{
            $this -> jsonReply('500' , '请填写与上次密码不同的新密码');
          }
      }
      else{
        $this -> jsonReply('500' , '您没有权限操作此功能');
      }
    }
  }
//删除检验
  public function delCheck(){
    if(IS_POST){
      $point_id = D('Point') -> addPoint('User/delCheck', '删除用户');//功能节点id
      $roleid = session('USER_AUTH.roleid');//权限角色id
      if( haveAblitity($point_id , $roleid) ){
        $userid = I('post.userid');
        if($userid=='1'){
            $this -> jsonReply('500' , '禁止删除');
        }
        $del = D('User') -> saveUser($userid,array('isdel'=>'1'));
        if($del){
          $this -> jsonReply('200' , '删除成功');
        }
        else{
          $this -> jsonReply('500' , '删除失败');
        }
      }
      else{
        $this -> jsonReply('500' , '您没有权限操作此功能');
      }
    }
  }


  //批量删除的检验
      public function delDatas(){
        if(IS_POST){
          $point_id = D('Point') -> addPoint('User/delDatas', '批量删除用户');//功能节点id
          $roleid = session('USER_AUTH.roleid');//权限角色id
          if( haveAblitity($point_id , $roleid) ){
            $id = I('post.id');
            if(in_array('1',$id)){
                $this -> jsonReply('500' , '禁止删除');
            }
            if(count($id)==1){
              $del = D('User') -> saveUser($id[0],array('isdel'=>'1'));
            }
            if(count($id)>1){
              foreach($id as $k=>$v){
                $del = D('User') -> saveUser($v,array('isdel'=>'1'));
              }
            }
            if($del){
              $this -> jsonReply('200' , '删除成功');
            }
            else{
              $this -> jsonReply('500' , '删除失败');
            }
          }
          else{
            $this -> jsonReply('500' , '您没有权限操作此功能');
          }
        }
      }


    public function zhi(){
        $type = I('post.id');
        $place = I('post.place');
        $user = D('User')->selectUsers(array('isdel'=>'0','level'=>$type,'place'=>$place));
        exit(json_encode($user));
    }























}
